package com.example.update.client;


import com.google.protobuf.InvalidProtocolBufferException;

import java.io.FileInputStream;
import java.io.IOException;

public class TestRead {
    public static void main(String[] args) throws IOException {
        // 从磁盘⽂件⾥读取，并反序列化为 Message 实例
        Contacts contacts = Contacts.parseFrom(
                new FileInputStream("src/main/java/com/example/update/contacts.bin"));
        // 打印
        printContacts(contacts);
    }

    private static void printContacts(Contacts contacts) throws InvalidProtocolBufferException {
        for (int i = 0; i < contacts.getContactsCount(); i++) {
            System.out.println("--------------联系⼈" + (i + 1) + "-----------");
            PeopleInfo peopleInfo = contacts.getContacts(i);
            System.out.println("姓名: " + peopleInfo.getName());
            System.out.println("年龄: " + peopleInfo.getAge());
            int j = 1;
            for (PeopleInfo.Phone phone : peopleInfo.getPhoneList()) {
                System.out.println("电话" + (j++) + ": " + phone.getNumber());
            }
            System.out.println("未知字段内容：\n" + peopleInfo.getUnknownFields());
        }
    }
}


